#include "header.h"

bool list_init(lpSeqList mylist, int length)
{
	if(!mylist)
	  return false;
	mylist->length = 0;
	mylist->max_length = length;
	if(!(mylist->list = (int *)malloc(sizeof(int)*length)))
	  return false;
	return true;
}

bool list_length(lpSeqList mylist,int* length)
{
	if(!mylist)
	  return false;
	*length = mylist->length;
	return true;
}

bool list_max_length(lpSeqList mylist,int* max_length)
{
	
	if(!mylist)
	  return false;
	*max_length = mylist->max_length;
	return true;
}

bool list_get(lpSeqList mylist, int locat,int* get)
{
	if(!mylist)
	  return false;
	if(mylist->length <= locat)
	  return false;
	if(locat < 0)
	  return false;
	*get = mylist->list[locat];
	return true;
}

bool list_prev(lpSeqList mylist, int locat,int* get)
{
	return list_get(mylist,locat-1,get);
}

bool list_next(lpSeqList mylist, int locat,int* get)
{
	return list_get(mylist,locat+1,get);
}

bool list_locate(lpSeqList mylist,int elem,int* locat)
{
	int i;
	bool flag = false;
	if(!mylist)
	  return false;
	i = mylist->length;
	while(i--)
	{
		if(elem == mylist->list[i])
		{
			flag = true;
			*locat = i;
			return true;
		}
	}
	return flag;
}

bool list_insert(lpSeqList mylist, int locat,int elem)
{
	int i;
	if(!mylist)
	  return false;
	if(mylist->length < locat)
	  return false;
	if((mylist->length == locat) && (mylist->max_length > locat))
	{
		mylist->list[locat] = elem;
		mylist->length++;
		return true;
	}

	for(i=mylist->length;i>locat;i--)
	{
		mylist->list[i] = mylist->list[i-1];
	}
	mylist->list[locat] = elem;
	mylist->length++;
	return true;
}

bool list_delet(lpSeqList mylist,int locat)
{
	int i;
	if(!mylist)
	  return false;
	if(mylist->length <= locat)
	  return false;
	for(i=locat;i<mylist->length;i++)
	  mylist->list[i] = mylist->list[i+1];
	mylist->length--;
	return true;
}

bool list_empty(lpSeqList mylist)
{
	if(!mylist)
	  return false;
	if(!memset(mylist->list,0,mylist->max_length))
	  return false;
	mylist->length = 0;
	return true;
}

bool list_isempty(lpSeqList mylist,bool* isempty)
{
	if(!mylist)
	  return false;
	if(0 == mylist->length)
		*isempty = true;
	else *isempty = false;
	return true;
}

bool list_free(lpSeqList mylist)
{
	if(!mylist)
	  return false;
	if(!mylist->list)
	  free(mylist->list);
	mylist->length = 0;
	mylist->max_length = 0;
	return true;
}




